/**
 * @file Bsp_Bmp280.c
 * @author 徐松亮 (5387603@qq.com)
 * @version 1.0
 * @date 2023-09-27
 *
 * @copyright Copyright (c) {2030}  沈阳迈思医疗科技有限公司
 *
 * @brief 驱动层-->环境气压温度传感器BMP280驱动程序。
 */
/* 私有头部文件 (Includes)-----------------------------------------------------*/
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include "Bsp_Bmp280.h"
/* 编译警告条件 (Compile warning condition)------------------------------------*/
/* 私有类型定义 (Private typedef)----------------------------------------------*/
// 工作模式
typedef enum
{
    BMP280_SLEEP_MODE = 0x0,  /*睡眠模式*/
    BMP280_FORCED_MODE = 0x1, /*触发模式*/
    BMP280_NORMAL_MODE = 0x3  /*正常模式*/
} BSP_BMP280_E_WORK_MODE;
// BMP压力过采样因子
typedef enum
{
    BMP280_P_MODE_SKIP = 0x0, /*skipped*/
    BMP280_P_MODE_1,          /*x1*/
    BMP280_P_MODE_2,          /*x2*/
    BMP280_P_MODE_3,          /*x4*/
    BMP280_P_MODE_4,          /*x8*/
    BMP280_P_MODE_5           /*x16*/
} BSP_BMP280_E_PRESS_OVERSAMPLING;
// BMP温度过采样因子
typedef enum
{
    BMP280_T_MODE_SKIP = 0x0, /*skipped*/
    BMP280_T_MODE_1,          /*x1*/
    BMP280_T_MODE_2,          /*x2*/
    BMP280_T_MODE_3,          /*x4*/
    BMP280_T_MODE_4,          /*x8*/
    BMP280_T_MODE_5           /*x16*/
} BSP_BMP280_E_TEMP_OVERSAMPLING;
// IIR滤波器时间常数
typedef enum
{
    BMP280_FILTER_OFF = 0x0, /*filter off*/
    BMP280_FILTER_MODE_1,
    /*0.223*ODR*/ /*x2*/
    BMP280_FILTER_MODE_2,
    /*0.092*ODR*/ /*x4*/
    BMP280_FILTER_MODE_3,
    /*0.042*ODR*/                      /*x8*/
    BMP280_FILTER_MODE_4 /*0.021*ODR*/ /*x16*/
} BSP_BMP280_E_FILTER_COEFFICIENT;
// 保持时间
typedef enum
{
    BMP280_T_SB1 = 0x0, /*0.5ms*/
    BMP280_T_SB2,       /*62.5ms*/
    BMP280_T_SB3,       /*125ms*/
    BMP280_T_SB4,       /*250ms*/
    BMP280_T_SB5,       /*500ms*/
    BMP280_T_SB6,       /*1000ms*/
    BMP280_T_SB7,       /*2000ms*/
    BMP280_T_SB8,       /*4000ms*/
} BSP_BMP280_E_TIME_SB;
//
typedef struct
{
    BSP_BMP280_E_PRESS_OVERSAMPLING P_Osample;
    BSP_BMP280_E_TEMP_OVERSAMPLING T_Osample;
    BSP_BMP280_E_WORK_MODE WORKMODE;
} BMP_OVERSAMPLE_MODE;
//
typedef struct
{
    BSP_BMP280_E_TIME_SB T_SB;
    BSP_BMP280_E_FILTER_COEFFICIENT FILTER_COEFFICIENT;
    FunctionalState SPI_EN;
} BMP_CONFIG;
/* 私有型宏定义 (Private define)-----------------------------------------------*/
#define BMP280_CHIPID_REG 0xD0           /*Chip ID Register */
#define BMP280_RESET_REG 0xE0            /*Softreset Register */
#define BMP280_STATUS_REG 0xF3           /*Status Register */
#define BMP280_CTRLMEAS_REG 0xF4         /*Ctrl Measure Register */
#define BMP280_CONFIG_REG 0xF5           /*Configuration Register */
#define BMP280_PRESSURE_MSB_REG 0xF7     /*Pressure MSB Register */
#define BMP280_PRESSURE_LSB_REG 0xF8     /*Pressure LSB Register */
#define BMP280_PRESSURE_XLSB_REG 0xF9    /*Pressure XLSB Register */
#define BMP280_TEMPERATURE_MSB_REG 0xFA  /*Temperature MSB Reg */
#define BMP280_TEMPERATURE_LSB_REG 0xFB  /*Temperature LSB Reg */
#define BMP280_TEMPERATURE_XLSB_REG 0xFC /*Temperature XLSB Reg */
#define BMP280_DIG_T1_LSB_REG 0x88
#define BMP280_DIG_T1_MSB_REG 0x89
#define BMP280_DIG_T2_LSB_REG 0x8A
#define BMP280_DIG_T2_MSB_REG 0x8B
#define BMP280_DIG_T3_LSB_REG 0x8C
#define BMP280_DIG_T3_MSB_REG 0x8D
#define BMP280_DIG_P1_LSB_REG 0x8E
#define BMP280_DIG_P1_MSB_REG 0x8F
#define BMP280_DIG_P2_LSB_REG 0x90
#define BMP280_DIG_P2_MSB_REG 0x91
#define BMP280_DIG_P3_LSB_REG 0x92
#define BMP280_DIG_P3_MSB_REG 0x93
#define BMP280_DIG_P4_LSB_REG 0x94
#define BMP280_DIG_P4_MSB_REG 0x95
#define BMP280_DIG_P5_LSB_REG 0x96
#define BMP280_DIG_P5_MSB_REG 0x97
#define BMP280_DIG_P6_LSB_REG 0x98
#define BMP280_DIG_P6_MSB_REG 0x99
#define BMP280_DIG_P7_LSB_REG 0x9A
#define BMP280_DIG_P7_MSB_REG 0x9B
#define BMP280_DIG_P8_LSB_REG 0x9C
#define BMP280_DIG_P8_MSB_REG 0x9D
#define BMP280_DIG_P9_LSB_REG 0x9E
#define BMP280_DIG_P9_MSB_REG 0x9F
// 复位寄存器写入值
#define BMP280_RESET_VALUE 0xB6
// 状态寄存器转换标志
#define BMP280_MEASURING 0x01
#define BMP280_IM_UPDATE 0x08

/* 私有变量定义 (Private variables)--------------------------------------------*/
/* 私有函数声明 (Private function prototypes)----------------------------------*/
static void BspBmp280_Cycle_100ms(void *_tp_interface);
/* 私有函数实体 (Private functions)--------------------------------------------*/
static int32_t t_fine;
// Returns temperature in DegC, resolution is 0.01 DegC. Output value of “5123” equals 51.23 DegC.
// t_fine carries fine temperature as global value
static int32_t bmp280_compensate_T_int32(BSP_BMP280_T_INTERFACE *_tp_interface, int32_t adc_T)
{
    int32_t var1, var2, T;
    var1 = ((((adc_T >> 3) - ((int32_t)_tp_interface->t_Private.t_Calibration.us_T1 << 1))) * ((int32_t)_tp_interface->t_Private.t_Calibration.s_T2)) >> 11;
    var2 = (((((adc_T >> 4) - ((int32_t)_tp_interface->t_Private.t_Calibration.us_T1)) * ((adc_T >> 4) - ((int32_t)_tp_interface->t_Private.t_Calibration.us_T1))) >> 12) *
            ((int32_t)_tp_interface->t_Private.t_Calibration.s_T3)) >>
           14;
    t_fine = var1 + var2;
    T = (t_fine * 5 + 128) >> 8;
    return T;
}

// Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24 integer bits and 8 fractional bits).
// Output value of “24674867” represents 24674867/256 = 96386.2 Pa = 963.862 hPa
static uint32_t bmp280_compensate_P_int64(BSP_BMP280_T_INTERFACE *_tp_interface, int32_t adc_P)
{
    int64_t var1, var2, p;
    var1 = ((int64_t)t_fine) - 128000;
    var2 = var1 * var1 * (int64_t)_tp_interface->t_Private.t_Calibration.s_P6;
    var2 = var2 + ((var1 * (int64_t)_tp_interface->t_Private.t_Calibration.s_P5) << 17);
    var2 = var2 + (((int64_t)_tp_interface->t_Private.t_Calibration.s_P4) << 35);
    var1 = ((var1 * var1 * (int64_t)_tp_interface->t_Private.t_Calibration.s_P3) >> 8) + ((var1 * (int64_t)_tp_interface->t_Private.t_Calibration.s_P2) << 12);
    var1 = (((((int64_t)1) << 47) + var1)) * ((int64_t)_tp_interface->t_Private.t_Calibration.us_P1) >> 33;
    if (var1 == 0)
    {
        return 0; // avoid exception caused by division by zero
    }
    p = 1048576 - adc_P;
    p = (((p << 31) - var2) * 3125) / var1;
    var1 = (((int64_t)_tp_interface->t_Private.t_Calibration.s_P9) * (p >> 13) * (p >> 13)) >> 25;
    var2 = (((int64_t)_tp_interface->t_Private.t_Calibration.s_P8) * p) >> 19;
    p = ((p + var1 + var2) >> 8) + (((int64_t)_tp_interface->t_Private.t_Calibration.s_P7) << 4);
    return (uint32_t)p;
}
/**************************传感器值转定点值*************************************/
// Returns temperature in DegC, double precision. Output value of “51.23” equals 51.23 DegC.
// t_fine carries fine temperature as global value
double bmp280_compensate_T_double(BSP_BMP280_T_INTERFACE *_tp_interface, int32_t adc_T)
{
    double var1, var2, T;
    var1 = (((double)adc_T) / 16384.0 - ((double)_tp_interface->t_Private.t_Calibration.us_T1) / 1024.0) * ((double)_tp_interface->t_Private.t_Calibration.s_T2);
    var2 = ((((double)adc_T) / 131072.0 - ((double)_tp_interface->t_Private.t_Calibration.us_T1) / 8192.0) *
            (((double)adc_T) / 131072.0 - ((double)_tp_interface->t_Private.t_Calibration.us_T1) / 8192.0)) *
           ((double)_tp_interface->t_Private.t_Calibration.s_T3);
    t_fine = (int32_t)(var1 + var2);
    T = (var1 + var2) / 5120.0;
    return T;
}

// Returns pressure in Pa as double. Output value of “96386.2” equals 96386.2 Pa = 963.862 hPa
double bmp280_compensate_P_double(BSP_BMP280_T_INTERFACE *_tp_interface, int32_t adc_P)
{
    double var1, var2, p;
    var1 = ((double)t_fine / 2.0) - 64000.0;
    var2 = var1 * var1 * ((double)_tp_interface->t_Private.t_Calibration.s_P6) / 32768.0;
    var2 = var2 + var1 * ((double)_tp_interface->t_Private.t_Calibration.s_P5) * 2.0;
    var2 = (var2 / 4.0) + (((double)_tp_interface->t_Private.t_Calibration.s_P4) * 65536.0);
    var1 = (((double)_tp_interface->t_Private.t_Calibration.s_P3) * var1 * var1 / 524288.0 + ((double)_tp_interface->t_Private.t_Calibration.s_P2) * var1) / 524288.0;
    var1 = (1.0 + var1 / 32768.0) * ((double)_tp_interface->t_Private.t_Calibration.us_P1);
    if (var1 == 0.0)
    {
        return 0; // avoid exception caused by division by zero
    }
    p = 1048576.0 - (double)adc_P;
    p = (p - (var2 / 4096.0)) * 6250.0 / var1;
    var1 = ((double)_tp_interface->t_Private.t_Calibration.s_P9) * p * p / 2147483648.0;
    var2 = p * ((double)_tp_interface->t_Private.t_Calibration.s_P8) / 32768.0;
    p = p + (var1 + var2 + ((double)_tp_interface->t_Private.t_Calibration.s_P7)) / 16.0;
    return p;
}

static uint8_t BspBmp280_Read_nByte(BSP_BMP280_T_INTERFACE *_tp_interface, uint8_t addr, uint8_t *buf, uint16_t num)
{
    uint8_t i;
    uint8_t res = false;
    _tp_interface->t_Input.tp_GpioIIC->t_Output.fnp_IIC_Start(_tp_interface->t_Input.tp_GpioIIC);
    _tp_interface->t_Input.tp_GpioIIC->t_Output.fnp_IIC_SendByte(_tp_interface->t_Input.tp_GpioIIC, _tp_interface->t_Private.uc_DeviceAddr | BSP_GPIO_IIC_WR);
    if (_tp_interface->t_Input.tp_GpioIIC->t_Output.fnp_IIC_WaitAck(_tp_interface->t_Input.tp_GpioIIC) != false)
    {
        res = true;
        goto BspBmp280_Read_nByte_GotoPoint;
    }
    _tp_interface->t_Input.tp_GpioIIC->t_Output.fnp_IIC_SendByte(_tp_interface->t_Input.tp_GpioIIC, addr);
    if (_tp_interface->t_Input.tp_GpioIIC->t_Output.fnp_IIC_WaitAck(_tp_interface->t_Input.tp_GpioIIC) != false)
    {
        res = true;
        goto BspBmp280_Read_nByte_GotoPoint;
    }
    //
    _tp_interface->t_Input.tp_GpioIIC->t_Output.fnp_IIC_Start(_tp_interface->t_Input.tp_GpioIIC);
    _tp_interface->t_Input.tp_GpioIIC->t_Output.fnp_IIC_SendByte(_tp_interface->t_Input.tp_GpioIIC, _tp_interface->t_Private.uc_DeviceAddr | BSP_GPIO_IIC_RD);
    if (_tp_interface->t_Input.tp_GpioIIC->t_Output.fnp_IIC_WaitAck(_tp_interface->t_Input.tp_GpioIIC) != false)
    {
        res = true;
        goto BspBmp280_Read_nByte_GotoPoint;
    }
    //
    for (i = 0; i < num; i++)
    {
        buf[i] = _tp_interface->t_Input.tp_GpioIIC->t_Output.fnp_IIC_ReadByte(_tp_interface->t_Input.tp_GpioIIC);
        if ((i + 1) == num)
        {
            _tp_interface->t_Input.tp_GpioIIC->t_Output.fnp_IIC_NAck(_tp_interface->t_Input.tp_GpioIIC);
        }
        else
        {
            _tp_interface->t_Input.tp_GpioIIC->t_Output.fnp_IIC_Ack(_tp_interface->t_Input.tp_GpioIIC);
        }
    }
BspBmp280_Read_nByte_GotoPoint:
    _tp_interface->t_Input.tp_GpioIIC->t_Output.fnp_IIC_Stop(_tp_interface->t_Input.tp_GpioIIC);
    return res;
}
static uint8_t BspBmp280_Write_nByte(BSP_BMP280_T_INTERFACE *_tp_interface, uint8_t *pbuf, uint16_t num)
{
    uint8_t i;
    uint8_t res = false;
    _tp_interface->t_Input.tp_GpioIIC->t_Output.fnp_IIC_Start(_tp_interface->t_Input.tp_GpioIIC);
    _tp_interface->t_Input.tp_GpioIIC->t_Output.fnp_IIC_SendByte(_tp_interface->t_Input.tp_GpioIIC, _tp_interface->t_Private.uc_DeviceAddr | BSP_GPIO_IIC_WR);
    if (_tp_interface->t_Input.tp_GpioIIC->t_Output.fnp_IIC_WaitAck(_tp_interface->t_Input.tp_GpioIIC) != false)
    {
        res = true;
        goto BspBmp280_Write_nByte_GotoPoint;
    }
    for (i = 0; i < num; i++)
    {
        _tp_interface->t_Input.tp_GpioIIC->t_Output.fnp_IIC_SendByte(_tp_interface->t_Input.tp_GpioIIC, pbuf[i]);
        if (_tp_interface->t_Input.tp_GpioIIC->t_Output.fnp_IIC_WaitAck(_tp_interface->t_Input.tp_GpioIIC) != false)
        {
            res = true;
            goto BspBmp280_Write_nByte_GotoPoint;
        }
    }
BspBmp280_Write_nByte_GotoPoint:
    _tp_interface->t_Input.tp_GpioIIC->t_Output.fnp_IIC_Stop(_tp_interface->t_Input.tp_GpioIIC);
    return res;
}

static uint8_t BspBmp280_Read_Calibration(BSP_BMP280_T_INTERFACE *_tp_interface)
{
    uint8_t *pbuf;
    uint8_t res = false;
    pbuf = ModuleMemManager_Get(E_MEM_MANAGER_TYPE_256B);
    res = BspBmp280_Read_nByte(_tp_interface, 0x88, pbuf, 24);
    if (res == false)
    {
        _tp_interface->t_Private.t_Calibration.us_T1 = MODULE_COUNT_2ByteToWord(pbuf[1], pbuf[0]);
        _tp_interface->t_Private.t_Calibration.s_T2 = MODULE_COUNT_2ByteToWord(pbuf[3], pbuf[2]);
        _tp_interface->t_Private.t_Calibration.s_T3 = MODULE_COUNT_2ByteToWord(pbuf[5], pbuf[4]);
        _tp_interface->t_Private.t_Calibration.us_P1 = MODULE_COUNT_2ByteToWord(pbuf[7], pbuf[6]);
        _tp_interface->t_Private.t_Calibration.s_P2 = MODULE_COUNT_2ByteToWord(pbuf[9], pbuf[8]);
        _tp_interface->t_Private.t_Calibration.s_P3 = MODULE_COUNT_2ByteToWord(pbuf[11], pbuf[10]);
        _tp_interface->t_Private.t_Calibration.s_P4 = MODULE_COUNT_2ByteToWord(pbuf[13], pbuf[12]);
        _tp_interface->t_Private.t_Calibration.s_P5 = MODULE_COUNT_2ByteToWord(pbuf[15], pbuf[14]);
        _tp_interface->t_Private.t_Calibration.s_P6 = MODULE_COUNT_2ByteToWord(pbuf[17], pbuf[16]);
        _tp_interface->t_Private.t_Calibration.s_P7 = MODULE_COUNT_2ByteToWord(pbuf[19], pbuf[18]);
        _tp_interface->t_Private.t_Calibration.s_P8 = MODULE_COUNT_2ByteToWord(pbuf[21], pbuf[20]);
        _tp_interface->t_Private.t_Calibration.s_P9 = MODULE_COUNT_2ByteToWord(pbuf[23], pbuf[22]);
    }
    ModuleMemManager_Free(E_MEM_MANAGER_TYPE_256B, pbuf);
    return res;
}

// 温度单位: 度*10 气压单位: pa 高度单位:米
/**
 * @brief
 * @param[in]  _tp_interface           My Param doc
 */
static void BspBmp280_GetData(BSP_BMP280_T_INTERFACE *_tp_interface)
{
    uint8_t res = false;
    uint8_t buf[3];
    int32_t i32;
    // 触发
    /*
    i = 0;
    while (1)
    {
        BspBmp280_Read_nByte(_tp_interface, BMP280_STATUS_REG, buf, 1);
        if ((buf[0] & BMP280_MEASURING) == 0)
        {
            break;
        }
        i++;
        if (i >= 10)
        {
            return true;
        }
    }
    */
    /*
    i = 0;
    while (1)
    {
        BspBmp280_Read_nByte(_tp_interface, BMP280_STATUS_REG, buf, 1);
        if ((buf[0] & BMP280_IM_UPDATE) == 0)
        {
            break;
        }
        i++;
        if (i >= 10)
        {
            return true;
        }
    }
    */
    // 延时
    //  MODULE_OS_DELAY_MS(10);
    // 读取压力
    res = BspBmp280_Read_nByte(_tp_interface, BMP280_PRESSURE_MSB_REG, buf, 3);
    if (res == true)
    {
        goto BspBmp280_GetData_GotoPoint;
    }
    i32 = MODULE_COUNT_4ByteToLong(0, buf[0], buf[1], buf[2]);
    i32 = i32 >> 4;
    // 转换压力
    i32 = bmp280_compensate_P_int64(_tp_interface, i32);
    _tp_interface->t_Output.ul_AirPressure = i32 / 256;
    // 读取温度
    res = BspBmp280_Read_nByte(_tp_interface, BMP280_TEMPERATURE_MSB_REG, buf, 3);
    if (res == true)
    {
        goto BspBmp280_GetData_GotoPoint;
    }
    i32 = MODULE_COUNT_4ByteToLong(0, buf[0], buf[1], buf[2]);
    i32 = i32 >> 4;
    // 转换温度
    i32 = bmp280_compensate_T_int32(_tp_interface, i32);
    _tp_interface->t_Output.l_Temp_x10 = i32 / 10;
    // 海拔换算
    _tp_interface->t_Output.s_Altitude = 44330.0 * (1.0 - pow((_tp_interface->t_Output.ul_AirPressure / 101325.0), (1.0 / 5.255)));
    // 赋值
BspBmp280_GetData_GotoPoint:
    if (res == true)
    {
        _tp_interface->t_Output.us_CommCmt_Err++;
        if (_tp_interface->t_Private.uc_InternalVariable_ErrCmt < UINT8_MAX)
        {
            _tp_interface->t_Private.uc_InternalVariable_ErrCmt++;
        }
        if (_tp_interface->t_Private.uc_InternalVariable_ErrCmt == 10)
        {
            _tp_interface->t_Output.uc_State = true;
            // 数值复位
            _tp_interface->t_Output.l_Temp_x10 = 0;
            _tp_interface->t_Output.s_Altitude = 0;
        }
    }
    else
    {
        _tp_interface->t_Private.uc_InternalVariable_ErrCmt = 0;
        _tp_interface->t_Output.us_CommCmt_Ok++;
        _tp_interface->t_Output.uc_State = false;
    }
    return;
}
static void BspBmp280_Cycle_100ms(void *_tp_interface)
{
    uint8_t *pbuf = NULL;
    BSP_BMP280_T_INTERFACE *tp_interface = _tp_interface;
    //
    tp_interface->t_Private.uc_Timer_100ms++;
    if (tp_interface->t_Private.uc_Timer_100ms % 10 != 0)
    {
        return;
    }
    tp_interface->t_Private.uc_Timer_100ms = 0;
    //
    switch (tp_interface->t_Private.uc_step)
    {
    case 0:
        // 采集芯片校准信息
        if (BspBmp280_Read_Calibration(tp_interface) == false)
        {
            tp_interface->t_Private.uc_step++;
        }
        break;
    case 1:
        // 复位芯片
        {
            uint8_t buf[2];
            buf[0] = BMP280_RESET_REG;
            buf[1] = BMP280_RESET_VALUE;
            if (BspBmp280_Write_nByte(tp_interface, buf, 2) == false)
            {
                tp_interface->t_Private.uc_step++;
            }
        }
        break;
    case 2:
        // 配置过采样
        {
            uint8_t buf[2];
            BMP_OVERSAMPLE_MODE BMP_OVERSAMPLE_MODEStructure;
            BMP_OVERSAMPLE_MODEStructure.P_Osample = BMP280_P_MODE_3;
            BMP_OVERSAMPLE_MODEStructure.T_Osample = BMP280_T_MODE_1;
            BMP_OVERSAMPLE_MODEStructure.WORKMODE = BMP280_NORMAL_MODE;
            buf[0] = BMP280_CTRLMEAS_REG;
            buf[1] = (BMP_OVERSAMPLE_MODEStructure.T_Osample << 5) | (BMP_OVERSAMPLE_MODEStructure.P_Osample << 2) | BMP_OVERSAMPLE_MODEStructure.WORKMODE;
            if (BspBmp280_Write_nByte(tp_interface, buf, 2) == false)
            {
                tp_interface->t_Private.uc_step++;
            }
        }
        break;
    case 3:
        // 配置滤波器
        {
            uint8_t buf[2];
            BMP_CONFIG BMP_CONFIGStructure;
            BMP_CONFIGStructure.T_SB = BMP280_T_SB1;
            BMP_CONFIGStructure.FILTER_COEFFICIENT = BMP280_FILTER_MODE_4;
            BMP_CONFIGStructure.SPI_EN = DISABLE;
            buf[0] = BMP280_CONFIG_REG;
            buf[1] = (BMP_CONFIGStructure.T_SB << 5) | (BMP_CONFIGStructure.FILTER_COEFFICIENT << 2) /*|BMP_CONFIGStructure.SPI_EN*/;
            if (BspBmp280_Write_nByte(tp_interface, buf, 2) == false)
            {
                tp_interface->t_Private.uc_step++;
            }
        }
        break;
    case 4:
        BspBmp280_GetData(tp_interface);
        break;
    default:
        break;
    }
    //
    if (tp_interface->t_Private.uc_DebugTestEnable == 1)
    {
        // 申请缓存
        pbuf = ModuleMemManager_Get(E_MEM_MANAGER_TYPE_256B);
        if (tp_interface->t_Private.uc_Flag_DebugFirst == 1)
        {
            tp_interface->t_Private.uc_Flag_DebugFirst = 0;
            //-----提取芯片ID(固定为55)
            BspBmp280_Read_nByte(tp_interface, BMP280_CHIPID_REG, &pbuf[250], 1);
            sprintf((char *)pbuf, "BMP280-FixID(0x58):0x%02x\r\n", pbuf[250]);
            tp_interface->t_Input.fnp_DebugOutStr((int8_t *)pbuf);
            //-----提取补偿寄存器
            sprintf((char *)pbuf, "BMP280 Calibration :\r\n");
            tp_interface->t_Input.fnp_DebugOutStr((int8_t *)pbuf);
            sprintf((char *)pbuf, "  us_T1:%d\r\n", tp_interface->t_Private.t_Calibration.us_T1);
            tp_interface->t_Input.fnp_DebugOutStr((int8_t *)pbuf);
            sprintf((char *)pbuf, "  s_T2:%d\r\n", tp_interface->t_Private.t_Calibration.s_T2);
            tp_interface->t_Input.fnp_DebugOutStr((int8_t *)pbuf);
            sprintf((char *)pbuf, "  s_T3:%d\r\n", tp_interface->t_Private.t_Calibration.s_T3);
            tp_interface->t_Input.fnp_DebugOutStr((int8_t *)pbuf);
            sprintf((char *)pbuf, "  us_P1:%d\r\n", tp_interface->t_Private.t_Calibration.us_P1);
            tp_interface->t_Input.fnp_DebugOutStr((int8_t *)pbuf);
            sprintf((char *)pbuf, "  s_P2:%d\r\n", tp_interface->t_Private.t_Calibration.s_P2);
            tp_interface->t_Input.fnp_DebugOutStr((int8_t *)pbuf);
            sprintf((char *)pbuf, "  s_P3:%d\r\n", tp_interface->t_Private.t_Calibration.s_P3);
            tp_interface->t_Input.fnp_DebugOutStr((int8_t *)pbuf);
            sprintf((char *)pbuf, "  s_P4:%d\r\n", tp_interface->t_Private.t_Calibration.s_P4);
            tp_interface->t_Input.fnp_DebugOutStr((int8_t *)pbuf);
            sprintf((char *)pbuf, "  s_P5:%d\r\n", tp_interface->t_Private.t_Calibration.s_P5);
            tp_interface->t_Input.fnp_DebugOutStr((int8_t *)pbuf);
            sprintf((char *)pbuf, "  s_P6:%d\r\n", tp_interface->t_Private.t_Calibration.s_P6);
            tp_interface->t_Input.fnp_DebugOutStr((int8_t *)pbuf);
            sprintf((char *)pbuf, "  s_P7:%d\r\n", tp_interface->t_Private.t_Calibration.s_P7);
            tp_interface->t_Input.fnp_DebugOutStr((int8_t *)pbuf);
            sprintf((char *)pbuf, "  s_P8:%d\r\n", tp_interface->t_Private.t_Calibration.s_P8);
            tp_interface->t_Input.fnp_DebugOutStr((int8_t *)pbuf);
            sprintf((char *)pbuf, "  s_P9:%d\r\n", tp_interface->t_Private.t_Calibration.s_P9);
            tp_interface->t_Input.fnp_DebugOutStr((int8_t *)pbuf);
        }
        if (tp_interface->t_Output.uc_State == false)
        {
            // 打印输出
            sprintf((char *)pbuf, "DebugOut(Comm Ok %05u,Err %05u): T-%ld.%ld AP-%lu(pa) A-%d(m)\r\n",
                    tp_interface->t_Output.us_CommCmt_Ok, tp_interface->t_Output.us_CommCmt_Err,
                    tp_interface->t_Output.l_Temp_x10 / 10, tp_interface->t_Output.l_Temp_x10 % 10,
                    tp_interface->t_Output.ul_AirPressure,
                    tp_interface->t_Output.s_Altitude);
            tp_interface->t_Input.fnp_DebugOutStr((int8_t *)pbuf);
        }
        else
        {
            // 打印输出
            sprintf((char *)pbuf, "DebugOut(Comm Ok %05u,Err %05u): ERROR\r\n",
                    tp_interface->t_Output.us_CommCmt_Ok, tp_interface->t_Output.us_CommCmt_Err);
            tp_interface->t_Input.fnp_DebugOutStr((int8_t *)pbuf);
        }
        // 释放缓存
        ModuleMemManager_Free(E_MEM_MANAGER_TYPE_256B, pbuf);
    }
    else
    {
        tp_interface->t_Private.uc_Flag_DebugFirst = 1;
    }
    //
}
/* 公有函数实体 (Public functions)---------------------------------------------*/
/**
 * @brief 初始化函数
 * @param[in]  _tp_interface           实体
 * @param[in]  iic_channel  IIC的通道号
 */
void BspBmp280_Init(BSP_BMP280_T_INTERFACE *_tp_interface)
{
    _tp_interface->t_Private.uc_DeviceAddr = BSP_BMP280_ADDR;
    _tp_interface->t_Transplant.fnp_Cycle_100ms = BspBmp280_Cycle_100ms;
}
/**
 * @brief 100ms周期执行函数
 * @param[in]  _tp_interface           实体
 */

/**
 * @brief 单元测试函数
 * @param[in]  OnOff        开关
 */
void BspBmp280_DebugTestOnOff(uint8_t _uc_trueOrFalse, void *_vp_context)
{
    BSP_BMP280_T_INTERFACE *tp_interface = _vp_context;

    if (tp_interface == NULL)
    {
        return;
    }
    if (_uc_trueOrFalse == true)
    {
        tp_interface->t_Private.uc_DebugTestEnable = 1;
    }
    else
    {
        tp_interface->t_Private.uc_DebugTestEnable = 0;
    }
}
/********************************END OF FILE***********************************/
